package com.example.idachuappone.index.adapter;

import java.io.Serializable;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.index.entity.FamilyFeastImgs;
import com.example.idachuappone.index.item.FamilyFeastTwoImageVpActivity;
import com.example.idachuappone.index.item.HotDetailGreedMenuImgActivity;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class AdapterHotGreedImg extends BaseAdapter {
	private BitmapUtils bu;
	private Context context;
	private List<FamilyFeastImgs> list;

	class AnonymousClass_3 implements OnClickListener {
		private final/* synthetic */int val$position;

		AnonymousClass_3(int i) {
			this.val$position = i;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(AdapterHotGreedImg.this.context, FamilyFeastTwoImageVpActivity.class);
			intent.putExtra("list", (Serializable) AdapterHotGreedImg.this.list);
			intent.putExtra("index", this.val$position);
			AdapterHotGreedImg.this.context.startActivity(intent);
		}
	}

	class AnonymousClass_4 implements OnClickListener {
		private final/* synthetic */int val$position;

		AnonymousClass_4(int i) {
			this.val$position = i;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(AdapterHotGreedImg.this.context, FamilyFeastTwoImageVpActivity.class);
			intent.putExtra("list", (Serializable) AdapterHotGreedImg.this.list);
			intent.putExtra("index", this.val$position);
			AdapterHotGreedImg.this.context.startActivity(intent);
		}
	}

	private class ViewHolder {
		ImageView img;
		LinearLayout ll_number;
		TextView tv_number;

		private ViewHolder() {
		}
	}

	public AdapterHotGreedImg(Context context) {
		this.context = context;
		this.bu = BitmapUtilHelper.createBitmapUtil(context);
	}

	public void setList(List<FamilyFeastImgs> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		int temp = this.list == null ? 0 : this.list.size();
		return temp >= 4 ? 4 : temp;
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (FamilyFeastImgs) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_hot_detail_greed_img_item, null);
			holder = new ViewHolder();
			holder.img = (ImageView) convertView.findViewById(R.id.img_item);
			holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
			holder.ll_number = (LinearLayout) convertView.findViewById(R.id.ll_number);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		this.bu.display(holder.img, this.list.get(position).getImg_small(), new BitmapLoadCallBack<View>() {
			@Override
			public void onLoadCompleted(View view, String arg1, Bitmap bitmap, BitmapDisplayConfig arg3,
					BitmapLoadFrom arg4) {
				((ImageView) view).setImageBitmap(bitmap);
			}

			@Override
			public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
			}
		});
		holder.ll_number.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		holder.img.setVisibility(0);
		if (this.list.size() <= 4) {
			holder.img.setOnClickListener(new AnonymousClass_4(position));
		} else if (position == 3) {
			holder.img.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			holder.ll_number.setVisibility(0);
			holder.tv_number.setText(new StringBuilder(String.valueOf(this.list.size())).toString());
			holder.ll_number.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent allIntent = new Intent(AdapterHotGreedImg.this.context, HotDetailGreedMenuImgActivity.class);
					allIntent.putExtra("list", (Serializable) AdapterHotGreedImg.this.list);
					AdapterHotGreedImg.this.context.startActivity(allIntent);
				}
			});
		} else {
			holder.img.setOnClickListener(new AnonymousClass_3(position));
		}
		return convertView;
	}
}
